package com.qfedu.channel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.channel.domain.Channel;
import com.qfedu.channel.mapper.ChannelMapper;
import com.qfedu.channel.service.IChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.QfConstant;
import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.aspectj.weaver.AjAttribute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangsan
 * @since 2023-03-01
 */
@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel> implements IChannelService {

    @Autowired
    ChannelMapper channelMapper;

    @Override
    public List<Channel> selectChannelList() {
        return channelMapper.selectChannelList();
    }

    /**
     * 实现添加渠道方法
     * @param channel
     * @return
     */
    @Override
    public AjaxResult createChannel(Channel channel) {
        //时间同意处理
        QueryWrapper<Channel> qw = new QueryWrapper<>();
        qw.lambda().eq(Channel::getChannelName, channel.getChannelName());
        Channel channel1 = getOne(qw);
        if (channel1 !=null){
            return AjaxResult.error("重复添加");
        }
        channel.setCreateTime(LocalDateTime.now());
        channel.setDeleteFlag(QfConstant.UNDELETED_FLAG);
        boolean save = save(channel);
//        if (save)

        return save ? AjaxResult.success("添加成功" ) : AjaxResult.error("添加失败");
    }

    /**
     * 删除
     * @param
     * @return
     */
    @Override
    public AjaxResult deleteUserByIds(Long[] channelIds) {
//        for (Long createId: createIds){
//            System.out.println(createId);
//        }
        boolean b = channelMapper.deleteChannelByids(channelIds);
        return b ? AjaxResult.success("删除成功" ) : AjaxResult.error("删除失败");
    }

    @Override
    public AjaxResult edit(Channel channel) {
        QueryWrapper<Channel> qw = new QueryWrapper<>();
        qw.lambda().eq(Channel::getChannelName,channel.getChannelName()).ne(Channel::getChannelId,channel.getChannelId());
        Channel channel1 = getOne(qw);
        if (channel1 != null){
            return AjaxResult.error("信息重复修改失败");
        }
        return channelMapper.edit(channel) ?AjaxResult.success("修改成功"):AjaxResult.error("修改失败");
    }

    @Override
    public Channel editById(Integer channelIds) {
        return channelMapper.editById(channelIds);
    }


}
